<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Projects;
use Illuminate\Http\Request;

class ProjectController extends Controller {

    public function index(Request $request){
        $params = [
            'kwd'  => (string)$request->get('title',''),
            'cateid'  => (int)$request->get('cateid',0),
            'status'  => (int)$request->get('status',0),
            'position'  => (int)$request->get('position',0),
            'start'  => (string)$request->get('start',''),
            'end'  => (string)$request->get('end',''),
        ];
        $lists = Projects::where(function ($query) use ($params) {
                if($params['kwd']) $query ->where('content','like','%' . $params['kwd'] . '%');
                if($params['status']) $query ->where('status',$params['status']);
                if($params['cateid']) $query ->where('cateid',$params['cateid']);
                if($params['position']) $query ->where('position',$params['position']);
                if($params['start']) $query ->whereDate('created_at','>=',$params['start']);
                if($params['end']) $query ->whereDate('created_at','<=',$params['end']);
            })->orderBy('created_at', 'desc')
            ->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])
            ->paginate(10);
        $this->stateToText($lists,[
            'status' => Projects::STATUS_TEXT,
            'position' => Projects::POSITION_TEXT,
        ]);
        return $this->writeJson(0,$lists);
    }


    //详情
    public function info(Request $request){
        $id = (string)$request->get('id','0');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        $info = Projects::findOrFail($id);
        $info['cateid'] = [$info['cateid']];
        if($info['about_project']){
            $projects = [];
            foreach($info['about_project'] as $item){
                $projects[] = (int)$item;
            }
            $info['about_project'] = $projects;
        }
        if($info['about_new']){
            $news = [];
            foreach($info['about_new'] as $item){
                $news[] = (int)$item;
            }
            $info['about_new'] = $news;
        }
        return $this->writeJson(0,$info);
    }

    //所有正常的新闻
    public function all(){
        $lists = Projects::select(['id','title'])->where('status',Projects::STATUS_1)
            ->orderBy('created_at', 'desc')->get();
        $projects = [];
        foreach($lists as $key=>$item){
            $projects[$key]['key'] = $item['id'];
            $projects[$key]['label'] = $item['title'];
        }
        return $this->writeJson(0,$projects);
    }


    //更新
    public function update(Request $request)
    {
        $data = $request->all();
        $data['cateid'] = $data['cateid'][0];
        if($data['id']){
            $info = Projects::findOrFail($data['id']);
            $info->update($data);
            return $this->writeJson(0,null,'修改项目信息成功');
        }else{
            unset($data['id']);
            Projects::create($data);
            return $this->writeJson(0,null,'新增项目信息成功');
        }
    }

    //删除
    public function destroy(Request $request){
        $id = $request->get('id');
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        if(is_array($id)){
            Projects::whereIn('id',$id)->delete();
        }else{
            Projects::destroy($id);
        }
        return $this->writeJson(0);
    }

}
